<?php declare(strict_types=1);

$di = \application\units\Di::getInstance();
['where' => $where, 'bindValue' => $bindValue, 'limit' => $limit, 'order' => $order] = $di->businessCommonQuery->build();

$total  = $di->db->find(sprintf('select count(*) total from admin_resource where 1=1 %s', $where), $bindValue)['total'];
$rows   = $di->db->query(sprintf('select * from admin_resource where 1=1 %s %s %s', $where, $order, $limit), $bindValue);

$fieldText = [
    'type'   => [1 => '菜单', 2 => '节点'],
    'status' => [1 => '正常', 0 => '隐藏'],
];
$ids    = array_column($rows, 'id');
$pids   = array_column($rows, 'pid');
$idDiff = array_diff($pids, $ids);
foreach ($rows as &$row) {
    $row['resource']    = $row['resource'] ?: str_replace('/', '.', $row['path']);
    $row['type_text']   = $fieldText['type'][$row['type']];
    $row['status_text'] = $fieldText['status'][$row['status']];
    $row['name']        = $row['title'];
    if (in_array($row['pid'], $idDiff)) {
        $row['pid'] = 0;
    }
}

return [
    'total' => $total,
    'rows'  => $rows,
];